package com.itheima.restkeeper.face;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.UserFace;
import com.itheima.restkeeper.pojo.Role;
import com.itheima.restkeeper.pojo.User;
import com.itheima.restkeeper.req.UserVo;
import com.itheima.restkeeper.service.IUserAdapterService;
import com.itheima.restkeeper.service.IUserService;
import com.itheima.restkeeper.utils.BeanConv;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserFaceImpl.java
 * @Description TODO
 */
@Slf4j
@DubboService(version = "${dubbo.application.version}",timeout = 5000,
        methods ={
                @Method(name = "findUserVoPage",retries = 2),
                @Method(name = "createUser",retries = 0),
                @Method(name = "updateUser",retries = 0),
                @Method(name = "deleteUser",retries = 0)
        })
public class UserFaceImpl implements UserFace {

    @Autowired
    IUserService userService;

    @Autowired
    IUserAdapterService userAdapterService;

    @Override
    public Page<UserVo> findUserVoPage(UserVo userVo, int pageNum, int pageSize) {
        //查询Page<User>用户分页
        Page<User> userPage = userService.findUserVoPage(userVo, pageNum, pageSize);
        //转化Page<User>为Page<UserVo>
        Page<UserVo> userVoPage = new Page<>();
        BeanConv.toBean(userPage,userVoPage);
        //转换List<User>为 List<UserVo>
        List<User> records = userPage.getRecords();
        List<UserVo> userVoList = BeanConv.toBeanList(records, UserVo.class);
        //查询角色信息
        userVoList.forEach(n->{
            //根据用户id查询角色集合
            List<Role> roles = userAdapterService.findRoleByUserId(n.getId());
            //从角色集合中提取出角色id集合
            List<String> roleIdList = new ArrayList<>();
            for (Role role : roles) {
                roleIdList.add(String.valueOf(role.getId()));
            }
            //将角色id集合转换成字符数组以便存储
            String[] roleIds = new String[roleIdList.size()];
            roleIdList.toArray(roleIds);
            //将角色id数组存储进用户中
            n.setHasRoleIds(roleIds);
        });
        userVoPage.setRecords(userVoList);
        //返回结果
        return userVoPage;
    }

    /**
     * 注册用户（新增用户）
     * @param userVo 对象信息
     * @return
     */
    @Override
    public UserVo createUser(UserVo userVo) {
        return BeanConv.toBean(userService.createUser(userVo),UserVo.class);
    }

    /**
     * 修改用户
     * @param userVo 对象信息
     * @return
     */
    @Override
    public Boolean updateUser(UserVo userVo) {
        return userService.updateUser(userVo);
    }

    /**
     * 根据点选id删除用户
     * @param checkedIds 选择对象信息Id
     * @return
     */
    @Override
    public Boolean deleteUser(String[] checkedIds) {
        return userService.deleteUser(checkedIds);
    }

    /**
     * 根据用户id查询用户
     * @param userId 选择对象信息Id
     * @return
     */
    @Override
    public UserVo findUserByUserId(Long userId) {
        return BeanConv.toBean(userService.getById(userId),UserVo.class);
    }

    /**
     * 查询用户集合
     * @return
     */
    @Override
    public List<UserVo> findUserVoList() {
        return BeanConv.toBeanList(userService.findUserVoList(),UserVo.class);
    }

}
